﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web;
using Telerik.Web.UI;
using portfolio.Business;
using portfolio.Common;

namespace portfolio.Views.DancersDBViews
{
    public partial class DancersDBAdmin1 : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            InitializeSession();
        }

        #region Helper Methods
        private void InitializeSession()
        {
            Initialize_rgDanceData();
        }

        private void Initialize_rgDanceData()
        {
            if (Session[SessionConstants.DancersDBAdminData] == null)
            {
                DancersDBBL dancersDBBL = new DancersDBBL();
                Session[SessionConstants.DancersDBAdminData] = dancersDBBL.GetAllSongs();
                rgDanceData.DataSource = (List<DancersDBData>)Session[SessionConstants.DancersDBAdminData];
            }
            else
            {
                rgDanceData.DataSource = (List<DancersDBData>)Session[SessionConstants.DancersDBAdminData];
            }
        }

        private void Initialize_rcbDanceFilter()
        {
            DancersDBBL dancersDBBL = new DancersDBBL();

            foreach (GridFilteringItem item in rgDanceData.MasterTableView.GetItems(GridItemType.FilteringItem))
            {
                RadComboBox rcb = (RadComboBox)item.FindControl(StringConstants.rcbDanceFilter);
                rcb.DataSource = dancersDBBL.GetAllDanceStyles();
                rcb.DataBind();
                rcb.Items.Insert(0, new RadComboBoxItem("Select All Styles", StringConstants.selectAllValues));
            }
        }

        private void ItemCommand_RebindGrid()
        {
            DancersDBBL dancersDBBL = new DancersDBBL();
            bool selectAllSongsChecked = false;

            foreach (GridFilteringItem item in rgDanceData.MasterTableView.GetItems(GridItemType.FilteringItem))
            {
                RadComboBox rcb = (RadComboBox)item.FindControl(StringConstants.rcbDanceFilter);
                List<string> danceStyles = new List<string>();

                if (rcb != null)
                {
                    if (rcb.CheckedItems.Count == 0 || rcb.SelectedIndex == 0)
                    {
                        Session[SessionConstants.DancersDBAdminData] = dancersDBBL.GetAllSongs();
                    }
                    else
                    {
                        foreach (RadComboBoxItem comboItem in rcb.CheckedItems)
                        {
                            selectAllSongsChecked = (comboItem.Value == StringConstants.selectAllValues);
                            if (selectAllSongsChecked) break;
                            danceStyles.Add(comboItem.Value);
                        }
                        if (!selectAllSongsChecked)
                        {
                            Session[SessionConstants.DancersDBAdminData] = dancersDBBL.GetAllSongsForDances(danceStyles);
                        }
                        else
                        {
                            Session[SessionConstants.DancersDBAdminData] = dancersDBBL.GetAllSongs();
                        }
                    }
                }
            }
            rgDanceData.DataSource = null;
            rgDanceData.Rebind();
        }

        private void ItemCommand_Edit(Telerik.Web.UI.GridCommandEventArgs e)
        {
            GridDataItem item = e.Item as GridDataItem;
            Session[SessionConstants.SongID] = int.Parse(item[StringConstants.SongID].Text);
        }

        private DancersDBData LoadEditableItem(GridEditableItem item, string command)
        {
            DancersDBData dancersDBData = new DancersDBData();
            DancersDBBL dancersDBBL = new DancersDBBL();

            string song = ((TextBox)item[StringConstants.Song].Controls[0]).Text;
            string artist = ((TextBox)item[StringConstants.Artist].Controls[0]).Text;
            string bpm = ((TextBox)item[StringConstants.BPM].Controls[0]).Text;
            string songLength = ((TextBox)item[StringConstants.SongLength].Controls[0]).Text;
            int BPM = 0;
            string songID = "";
            if (command == StringConstants.Update || command == StringConstants.Delete)
            {
                songID = ((TextBox)item[StringConstants.SongID].Controls[0]).Text;
            }

            if (!(String.IsNullOrEmpty(song) || String.IsNullOrWhiteSpace(song)))
            {
                if (command == StringConstants.Update || command == StringConstants.Delete)
                {
                    dancersDBData.SongID = int.Parse(songID);
                }
                dancersDBData.Song = song;
                dancersDBData.Artist = artist;
                if (int.TryParse(bpm, out BPM))
                {
                    dancersDBData.BPM = BPM;
                }
                else
                {
                    dancersDBData.BPM = null;
                }
                dancersDBData.SongLength = songLength;
                dancersDBData.SongLengthInSeconds = dancersDBData.ConvertSongLengthToSeconds(songLength);

                RadComboBox rcb = (RadComboBox)item.FindControl(StringConstants.rcbEditDanceFilter);
                if (rcb != null)
                {
                    foreach (var dance in rcb.CheckedItems)
                    {
                        DanceStyleData danceStyle = new DanceStyleData();
                        if (dance.Checked)
                        {
                            danceStyle.DanceIdentifier = dance.Value;
                            danceStyle.DanceName = dance.Text;
                            dancersDBData.DanceStylesList.Add(danceStyle);
                        }
                    }
                }
            }
            return dancersDBData;
        }
        #endregion

        #region Event Handlers
        protected void rgDanceData_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            if (Session[SessionConstants.DancersDBAdminData] != null)
            {
                rgDanceData.DataSource = (List<DancersDBData>)Session[SessionConstants.DancersDBAdminData];
            }
            else
            {
                DancersDBBL dancersDBBL = new DancersDBBL();
                Session[SessionConstants.DancersDBAdminData] = dancersDBBL.GetAllSongs();
                rgDanceData.DataSource = (List<DancersDBData>)Session[SessionConstants.DancersDBAdminData];
            }
        }

        protected void rgDanceData_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case StringConstants.Edit:
                    {
                        ItemCommand_Edit(e);
                        break;
                    }
                case StringConstants.RebindGrid:
                    {
                        ItemCommand_RebindGrid();
                        break;
                    }
                default:
                    {
                        break;
                    }
            }

        }

        protected void rgDanceData_PreRender(object sender, EventArgs e)
        {
            Initialize_rcbDanceFilter();
        }

        protected void rgDanceData_InsertCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
        {
            GridEditableItem item = e.Item as GridEditableItem;

            if (item != null)
            {
                DancersDBBL dancersDBBL = new DancersDBBL();
                DancersDBData dancersDBData = LoadEditableItem(item, StringConstants.Insert);
                dancersDBBL.InsertSong(dancersDBData);
            }
            ItemCommand_RebindGrid();
        }

        protected void rgDanceData_UpdateCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
        {
            GridEditableItem item = e.Item as GridEditableItem;

            if (item != null)
            {
                DancersDBBL dancersDBBL = new DancersDBBL();
                DancersDBData dancersDBData = LoadEditableItem(item, StringConstants.Update);
                dancersDBBL.UpdateSong(dancersDBData);
            }
            ItemCommand_RebindGrid();
        }

        protected void rgDanceData_DeleteCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
        {
            GridDataItem item = e.Item as GridDataItem;
            int songID = int.Parse(item.GetDataKeyValue(StringConstants.SongID).ToString());
            DancersDBBL dancersDBBL = new DancersDBBL();
            dancersDBBL.DeleteSong(songID);
            
            ItemCommand_RebindGrid();
        }

        protected void rgDanceData_ItemCreated(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridEditableItem && e.Item.IsInEditMode)
            {
                DancersDBBL dancersDBBL = new DancersDBBL();
                GridEditableItem item = e.Item as GridEditableItem;
                RadComboBox rcb = (RadComboBox)item.FindControl(StringConstants.rcbEditDanceFilter);

                if (rcb != null)
                {
                    rcb.DataSource = dancersDBBL.GetAllDanceStyles();
                    rcb.DataBind();
                }
            }
        }

        protected void rgDanceData_ItemDataBound(object sender, GridItemEventArgs e)
        {
            DancersDBBL dancersDBBL = new DancersDBBL();
            GridEditableItem item = e.Item as GridEditableItem;
            if (item != null)
            {
                RadComboBox rcb = (RadComboBox)item.FindControl(StringConstants.rcbEditDanceFilter);

                if (rcb != null)
                {
                    if (Session[SessionConstants.SongID] != null)
                    {
                        List<DanceStyleData> danceStyles = dancersDBBL.GetAllDanceStylesForSong(int.Parse(Session[SessionConstants.SongID].ToString()));

                        foreach (DanceStyleData dance in danceStyles)
                        {
                            foreach (RadComboBoxItem comboItem in rcb.Items)
                            {
                                if (comboItem.Value == dance.DanceIdentifier)
                                {
                                    comboItem.Checked = true;
                                }
                            }
                        }
                    }
                }
            }
        }
        #endregion
    }
}